package com.jhgsys.internal.incentive.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.jhgsys.internal.incentive.constant.IncentiveRewardConstant;
import com.jhgsys.internal.incentive.entity.IncentiveRewardClause;
import com.jhgsys.internal.incentive.mapper.IncentiveRewardClauseMapper;
import com.jhgsys.internal.incentive.service.IIncentiveRewardClauseService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 奖励条款 Service实现
 *
 * @author zjb
 * @date 2024-08-30 09:51:04
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class IncentiveRewardClauseServiceImpl extends ServiceImpl<IncentiveRewardClauseMapper, IncentiveRewardClause> implements IIncentiveRewardClauseService {

    /**
     * 查询（分页）
     * @author zjb
     * @date 2024-08-30 09:51:04
     * @param request QueryRequest
     * @param incentiveRewardClause incentiveRewardClause
     * @return IPage<IncentiveRewardClause>
    */
    @Override
    public IPage<IncentiveRewardClause> findIncentiveRewardClausePage(QueryRequest request, IncentiveRewardClause incentiveRewardClause) {
        LambdaQueryWrapper<IncentiveRewardClause> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IncentiveRewardClause::getDeletedFlg, 0);
        // TODO 设置查询条件
        if(StringUtils.isNotBlank(incentiveRewardClause.getClauseName())){
            queryWrapper.like(IncentiveRewardClause::getClauseName,incentiveRewardClause.getClauseName());
        }
        if(StringUtils.isNotBlank(incentiveRewardClause.getTenantId())){
            queryWrapper.eq(IncentiveRewardClause::getTenantId,incentiveRewardClause.getTenantId());
        }
        if(StringUtils.isNotBlank(incentiveRewardClause.getStatus())){
            queryWrapper.eq(IncentiveRewardClause::getStatus,incentiveRewardClause.getStatus());
        }
        if(StringUtils.isNotBlank(incentiveRewardClause.getClauseName())){
            queryWrapper.like(IncentiveRewardClause::getClauseName,incentiveRewardClause.getClauseName());
        }
        if(StringUtils.isNotBlank(incentiveRewardClause.getUseScope())){
            queryWrapper.and(item->item.eq(IncentiveRewardClause::getUseScope,incentiveRewardClause.getUseScope()).or().eq(IncentiveRewardClause::getUseScope, IncentiveRewardConstant.USE_SCOPE.ALL));
        }
        queryWrapper.orderByAsc(IncentiveRewardClause::getSort);
        Page<IncentiveRewardClause> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author zjb
     * @date 2024-08-30 09:51:04
     * @param incentiveRewardClause incentiveRewardClause
     * @return List<IncentiveRewardClause>
     */
    @Override
    public List<IncentiveRewardClause> findIncentiveRewardClauseList(IncentiveRewardClause incentiveRewardClause) {
	    LambdaQueryWrapper<IncentiveRewardClause> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IncentiveRewardClause::getDeletedFlg, 0);
		// TODO 设置查询条件
        if(StringUtils.isNotBlank(incentiveRewardClause.getTenantId())){
            queryWrapper.eq(IncentiveRewardClause::getTenantId,incentiveRewardClause.getTenantId());
        }
        if(StringUtils.isNotBlank(incentiveRewardClause.getClauseName())){
            queryWrapper.eq(IncentiveRewardClause::getClauseName,incentiveRewardClause.getClauseName());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author zjb
    * @date 2024-08-30 09:51:04
    * @param id
    * @return IncentiveRewardClause
    */
    @Override
    public IncentiveRewardClause findIncentiveRewardClauseById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author zjb
     * @date 2024-08-30 09:51:04
     * @param incentiveRewardClause incentiveRewardClause
     */
    @Override
    @Transactional
    public void createIncentiveRewardClause(IncentiveRewardClause incentiveRewardClause) {
        incentiveRewardClause.setCreatedTime(new Date());
        this.save(incentiveRewardClause);
    }

    /**
     * 修改
     * @author zjb
     * @date 2024-08-30 09:51:04
     * @param incentiveRewardClause incentiveRewardClause
     */
    @Override
    @Transactional
    public void updateIncentiveRewardClause(IncentiveRewardClause incentiveRewardClause) {
        incentiveRewardClause.setUpdatedTime(new Date());
        this.saveOrUpdate(incentiveRewardClause);
    }

    /**
     * 逻辑删除
     * @author zjb
     * @date 2024-08-30 09:51:04
     * @param incentiveRewardClause incentiveRewardClause
     * @param incentiveRewardClauseIds incentiveRewardClauseIds
     */
    @Override
    @Transactional
    public void deleteIncentiveRewardClause(IncentiveRewardClause incentiveRewardClause,String incentiveRewardClauseIds) {
        List<String> list = Arrays.asList(incentiveRewardClauseIds.split(StringPool.COMMA));
        this.baseMapper.update(incentiveRewardClause,new LambdaQueryWrapper<IncentiveRewardClause>().in(IncentiveRewardClause::getClauseId,list));
	}

    /**
    * 物理删除
    * @author zjb
    * @date 2024-08-30 09:51:04
    * @param incentiveRewardClauseIds incentiveRewardClauseIds
    */
    @Override
    @Transactional
    public void physicallyDeleteIncentiveRewardClause(String incentiveRewardClauseIds) {
        List<String> list = Arrays.asList(incentiveRewardClauseIds.split(StringPool.COMMA));
        LambdaQueryWrapper<IncentiveRewardClause> wapper = new LambdaQueryWrapper<>();
        wapper.in(IncentiveRewardClause::getClauseId,list);
        this.remove(wapper);
    }
}
